package com.office.forum.manage.article.controller;

import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

import com.office.forum.manage.comments.domain.ProcessComments;
import com.office.web.controller.common.CommonController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.office.common.annotation.Log;
import com.office.common.core.controller.BaseController;
import com.office.common.core.domain.AjaxResult;
import com.office.common.enums.BusinessType;
import com.office.forum.manage.article.domain.ForumPosts;
import com.office.forum.manage.article.service.IForumPostsService;
import com.office.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import static com.office.forum.manage.comments.domain.ProcessComments.processComments;

/**
 * 帖子Controller
 * 
 * @author ruoyi
 * @date 2025-05-16
 */
@RestController
@RequestMapping("/posts/posts")
@Api( "帖子管理")
public class ForumArticleController extends BaseController
{
    @Autowired
    private IForumPostsService forumPostsService;


    /**
     * 查询帖子列表
     */
    @ApiOperation("分页查询")
    @GetMapping("/list")
    public TableDataInfo list(ForumPosts forumPosts)
    {
        startPage();
        List<ForumPosts> list = forumPostsService.selectForumPostsList(forumPosts);
        if (list == null)
        {
            list = new ArrayList<>();
        }
        return getDataTable(list);
    }


    /**
     * 获取帖子详细信息
     */
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable("postId") Long postId)
    {
        return success(forumPostsService.selectForumPostsByPostId(postId));
    }

    /**
     * 新增帖子
     */
    @Log(title = "帖子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ForumPosts forumPosts)
    {

        return toAjax(forumPostsService.insertForumPosts(forumPosts));
    }

    /**
     * 修改帖子
     */
    @Log(title = "帖子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ForumPosts forumPosts)
    {
        return toAjax(forumPostsService.updateForumPosts(forumPosts));
    }

    /**
     * 删除帖子
     */
    @Log(title = "帖子", businessType = BusinessType.DELETE)
	@DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {
        return toAjax(forumPostsService.deleteForumPostsByPostIds(postIds));
    }

    /**
     *获取下拉分类名
     * @return
     */
    @GetMapping("/category")
    public AjaxResult category(){
        return AjaxResult.success(forumPostsService.getCategoryName());
    }

    /**
     * 根据id找到此id发布的帖子
     */
    @GetMapping("/query")
    public TableDataInfo getUserInfo(ForumPosts forumPosts)
    {
        startPage();
        Long userId = forumPosts.getUserId();
        List<ForumPosts> list = forumPostsService.selectForumPostsByUserId(userId);
        return getDataTable(list);
    }
    /**
     * id批量查询收藏的帖子
     */
    // GET /api/posts?postIds=1,2,3
    @GetMapping("/getLikesByIds")
    public ResponseEntity<?> getPostsByIds(@RequestParam List<Long> postIds) {
        List<ForumPosts> posts = forumPostsService.getPostsByIds(postIds);
        return ResponseEntity.ok().body(posts);
    }
}
